class UserController < ApplicationController
  
  include ApplicationHelper
  
  before_filter :protect, :except => [:login, :register]
  
  def share_confirm
  end
  
  def share
    doc_id = params[:docid]
    min_time = request.env["HTTP_IF_MODIFIED_SINCE"]
    if (min_time != nil)
      render_text '', '304 Not Modified'
    else
      @user = Share.get_user_from_session(session)
      doc_id = params[:docid]
      @source_data = @user.share.get_doc(doc_id, "src") 
      if (@source_data != nil)
        response.headers['Content-Description'] = doc_id
        response.headers['Last-Modified'] = "Mon, 1 Oct 2007 14:19:41 GMT"
        response.headers['Cache-Control'] = "max-age=31536000, must-revalidate"     
        response.headers['Expires'] = "Sat, 1 Jan 2011 14:19:41 GMT"     
        send_data(@source_data, :disposition => "inline")
      else
        render_text '', "500 Server failed to return source document"
      end
    end
  end
    
  def thumb
     min_time = request.env["HTTP_IF_MODIFIED_SINCE"]
     if (min_time != nil)
       render_text '', '304 Not Modified'
     else
       @user = Share.get_user_from_session(session)
       doc_id = params[:docid]
       @thumbnail_data = @user.share.get_doc(doc_id, "thumbnail") 
       if (@thumbnail_data != nil)
         response.headers['Content-Description'] = doc_id
         response.headers['Last-Modified'] = "Mon, 1 Oct 2007 14:19:41 GMT"
         response.headers['Cache-Control'] = "max-age=31536000, must-revalidate"     
         response.headers['Expires'] = "Sat, 1 Jan 2011 14:19:41 GMT"     
         send_data(@thumbnail_data, :type => "image/jpg", :disposition => "inline")
       else
         redirect_to "/images/MIA.jpg"
         # render_text '', "500 Server failed to return thumbnail"
       end
     end
  end
  
  def src
     mime = params[:mime]
     if (mime == "pdf")
       mimetype = "application/pdf"
     elsif (mime == "doc")
       mimetype = "application/word"
     elsif (mime == "ppt")
       mimetype = "application/powerpoint"
     elsif (mime == "png")
       mimetype = "image/png"
     elsif (mime == "jpg")
       mimetype = "image/jpg"
     elsif (mime == "gif")
       mimetype = "image/gif"
     else
       mimetype = "application/octet_stream"
     end
     min_time = request.env["HTTP_IF_MODIFIED_SINCE"]
     if (min_time != nil)
       render_text '', '304 Not Modified'
     else
       @user = Share.get_user_from_session(session)
       doc_id = params[:docid]
       @source_data = @user.share.get_doc(doc_id, "src") 
       if (@source_data != nil)
         response.headers['Content-Description'] = doc_id
         response.headers['Last-Modified'] = "Mon, 1 Oct 2007 14:19:41 GMT"
         response.headers['Cache-Control'] = "max-age=31536000, must-revalidate"     
         response.headers['Expires'] = "Sat, 1 Jan 2011 14:19:41 GMT"     
         send_data(@source_data, :type => "#{mimetype}",:disposition => "inline")
       else
         render_text '', "500 Server failed to return source document"
       end
     end
  end

  def index
    @title = "Share PDF User Page"
    @user = Share.get_user_from_session(session)
  end
  
  def links
    @title = "Share PDF User Page"
    @user = Share.get_user_from_session(session)
  end

  def register
    @title = "Share PDF User Registeration Page"
  end
  
  def login
    @title = "Sign In Share PDF"
    if request.post? and params[:user]
      #logger.info params[:user].inspect
      #raise params[:user].inspect
      arg = params[:user]
      @user = User.new(arg["email"], arg["password"])
      user = User.loginAdobe(@user)
      if user
        if (user.share.auth_token != nil)
          @user.share = user.share
          flash[:notice] = "Welcome back #{@user.share.user_full_name}! <br/>You've #{@user.share.doc_nodes.length} documents in your library"
          user.login!(session)
          if @user.remember_me == "1"
            cookies[:adobeid] = { :value => "#{user.email}", :expires => 10.years.from_now }
          end
          redirect_to_next_page
        else
          @user.password = nil
          flash[:notice] = "Share server returned: #{user.share.http_response_code} #{user.share.http_response_message}. Please try again later."          
        end
      else
        @user.password = nil
        flash[:notice] = "Invalid email & password combination."
      end
    end
  end
  
  def logout
    session[:user] = nil
    flash[:notice] = "You've successfully loggged out"
    redirect_to :action => "index", :controller => "site"
  end
  
  private
  
  def protect
    unless logged_in?
      session[:protected_page] = request.request_uri
      flash[:notice] = "Please log in first."
      redirect_to :action => "login"
      return false
    end
  end
  
  def redirect_to_next_page
    if (next_url = session[:protected_page])
      redirect_to next_url
      session[:protected_page] = nil
    else
      redirect_to :action => "index"
    end    
  end
  
end
